package ltd.bugs.cute.mybatis;

import java.util.Set;

import org.apache.ibatis.type.Alias;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import ltd.bugs.cute.common.reflect.ReflectionsUtils;

/**
 * 自动识别所有注解了@Alias的model，并自动设置别名
 * 
 * @author zhengzhq
 *
 */
public class AliaseSqlSessionFactoryBean extends SqlSessionFactoryBean {

  private static Logger logger = LoggerFactory.getLogger(AliaseSqlSessionFactoryBean.class);

  @Override
  public void setTypeAliases(Class<?>[] typeAliases) {
    Set<Class<?>> classSet = ReflectionsUtils.getTypesAnnotatedWith(Alias.class);
    if (!CollectionUtils.isEmpty(classSet)) {
      logger.info("@Aliase classes as follow: ");
      for (Class<?> classz : classSet) {
        logger.info("        " + classz.getName());
      }
    } else {
      logger.info("@Aliase classes is empty!");
    }
    super.setTypeAliases(classSet.toArray(new Class<?>[] {}));
  }
}
